var express = require('express'),
    router = express.Router(),    
    formidable = require('formidable'),
      fs = require('fs'),
      TITLE = 'formidable上传示例',
    AVATAR_UPLOAD_FOLDER = '/avatar/',
    util = require('util');

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: TITLE });
});

router.post('/', function(req, res) {


  var form = new formidable.IncomingForm();   //创建上传表单
      form.encoding = 'utf-8';        //设置编辑
      form.uploadDir = 'public' + AVATAR_UPLOAD_FOLDER;     //设置上传目录
      form.keepExtensions = false;     //保留后缀
      form.maxFieldsSize = 2 * 1024 * 1024;   //文件大小

    form.parse(req, function(err, fields, files) {

      res.writeHead(200, {'content-type': 'text/plain'});

      res.write('received upload:\n\n');

      res.end(util.inspect({fields: fields, files: files}));

    });

    res.locals.success = '上传成功';
    res.render('index', { title: TITLE });      
});

module.exports = router;